'
' DNN Content Builder - sourceforge.net/projects/dnncb
' Copyright (c) 2004-2007
' by Emerson Lopes (emersonlopes@gmail.com)
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
'
Imports System.Web.UI
Imports System.Xml
Imports DNNContentBuilder
Imports DNNContentBuilder.Attributes
Imports DNNContentBuilder.UserControls.SearchFieldControls


<FormEngineSearchControlAttribute(GetType(TextSearchControl))> _
Public Class TextBoxIIControl
    Inherits DNNContentBuilder.UserControls.FieldControls.BaseFieldControl
    Implements DNNContentBuilder.IFormlet

    Public Const resource As String = "~/DesktopModules/DNNContentBuilder/App_LocalResources/TextBoxIIControl"

    Protected xmlDoc As New XmlDocument
    Protected WithEvents tbClass As System.Web.UI.WebControls.Literal
    Private isValidProperty As Boolean = True

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents regexValidator As System.Web.UI.WebControls.RegularExpressionValidator
    Protected WithEvents txtText As System.Web.UI.WebControls.TextBox

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
    End Sub

    Public Function GetValue(ByVal XML As String) As String Implements DNNContentBuilder.IFormlet.GetValue
        Dim xmlDoc As New XmlDocument

        xmlDoc.LoadXml(XML)

        Dim xmlNode As XmlNode = xmlDoc.SelectSingleNode("//SystemType")

        xmlNode.InnerText = "String"

        xmlNode = xmlDoc.SelectSingleNode("//Value")

        Me.isValidProperty = True

        xmlNode.InnerText = txtText.Text

        Return xmlDoc.InnerXml
    End Function

    Public ReadOnly Property IsValid() As Boolean Implements DNNContentBuilder.IFormlet.IsValid
        Get
            Return Me.isValidProperty
        End Get
    End Property

    Public Sub SetConfig(ByVal XML As String) Implements DNNContentBuilder.IFormlet.SetConfig
        Dim xmlDoc As New XmlDocument
        Dim tempNode As XmlNode

        xmlDoc.LoadXml(XML)

        tempNode = xmlDoc.SelectSingleNode("//Size")

        If (tempNode.InnerText <> "") Then
            txtText.Columns = Int32.Parse(tempNode.InnerText)
        Else
            txtText.Columns = 10
        End If

        tempNode = xmlDoc.SelectSingleNode("//MaxLength")

        If (tempNode.InnerText <> "") Then
            txtText.MaxLength = Int32.Parse(tempNode.InnerText)
        End If

        tempNode = xmlDoc.SelectSingleNode("//Style")

        If (tempNode.InnerText <> "") Then
            Me.tbClass.Text = "<style>.textBoxIIClass {" & tempNode.InnerText & "}</style>"
            txtText.CssClass = "textBoxIIClass"
        End If

        'recovering regular expression for this field
        tempNode = xmlDoc.SelectSingleNode("//RegExp")

        Dim regex As String = tempNode.InnerText

        If (regex Is Nothing And regex.Length > 0) Then
            regexValidator.ErrorMessage = String.Format("<image src='{0}' border=0 alt='{1}'/>", Page.ResolveUrl("~/DesktopModules/DNNContentBuilder/images/icon_erro.gif"), ResourceEngine.GetString("ERR_INVALID_FORM_DATA"))
            regexValidator.ValidationExpression = regex
            regexValidator.Enabled = True
        End If

        txtText.Text = String.Empty
    End Sub

    Public Sub SetValue(ByVal XML As String) Implements DNNContentBuilder.IFormlet.SetValue
        Dim xmlDoc As New XmlDocument
        xmlDoc.LoadXml(XML)

        Dim xmlNode As XmlNode = xmlDoc.SelectSingleNode("//Value")
        txtText.Text = xmlNode.InnerText
    End Sub
End Class
